<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="author" content="Simon Wikander" />
        <link rel="stylesheet" type="text/css" href="./style/style.css" />
        <title>Bfg example!</title>
        <?php
        $example = null;
        if (isset($_GET['example'])) {
            $example = $_GET['example'];
        } else {
            //get the last edited example!
            $timestamp = null;
            foreach (new DirectoryIterator("./bfg_examples/") as $fileInfo) {
                if (!$fileInfo->isFile())
                    continue;
                $fullFilename = $fileInfo->getFilename();
                $extension = pathinfo($fullFilename, PATHINFO_EXTENSION);
                if ($extension === "svg") {
                    if (($fileInfo->getMTime() > $timestamp) || ($timestamp == null)) {
                        $filename = pathinfo($fullFilename, PATHINFO_FILENAME);
                        $example = $filename;
                        $timestamp = $fileInfo->getMTime();
                    }
                }
            }
        }
        $bfgExample = "./bfg_examples/" . $example . ".svg";
        $bpExample = "./bp_examples/" . $example . ".bp";
        
        $view = null;
        if (isset($_GET['view'])) {
            $view = "#" . $_GET['view'];
        } else {
            $view = "#bfg";
        }
        
        $presentaionClassString = "";
            if (isset($_GET['presentation'])) {
                 if ($_GET['presentation'] == "yes") {
                    $presentaionClassString = "$('body').addClass('presentation');\n$('#presentationVar').val('yes');\n";
                } else  { //assumed to be "no"
                $presentaionClassString = "$('#presentationVar').val('no');\n";
                }
                }

        ?>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">  
<?php // change to jquery with $(window).height();   // returns height of browser viewport  ?>
            function rearrange()
            {
                var windowHeight = $(window).height();
                var offset = (windowHeight*0.08);
                $("#bfg").height(windowHeight - offset);
                $("#bp").height(windowHeight - offset);
            }
            
            var flipObject = "<?php echo $view; ?>";
            

            $(document).ready(function() {  
                <?=$presentaionClassString?>
                $(window).resize(function() {
                    rearrange();
                });
  
                $("#closeNote").click(function() {
                    $("#note").hide('fast');
                    $("#tooltipVar").val("no");
                });
    
                $("#example").change(function() {
                    $(this).closest("form").submit();
                });
  
                $("#flip").click(function () {
              $(window).scrollTop($((flipObject)).offset().top - 40);
              if (flipObject == "#bfg") {
                  flipObject = "#bp";
                  $("#viewVar").val("bfg");
              } else {
                  flipObject = "#bfg";
                  $("#viewVar").val("bp");
              }
            });
            rearrange();
            $("#flip").trigger('click');
            <?php
                if (isset($_GET['tooltips'])) {
                    if ($_GET['tooltips'] == "no") {
                        echo "$('.tooltip').hide();\n$('#tooltipVar').val('no');";
                    }
                }
                ?>
            });
        </script>
    </head>

    <body>
        <div class="left_menu" id="flip">
            <div>Flip view</div>
        </div>

        <div class="right_menu"><div>More examples:
            <form method="get" action="./index.php" style="display: inline;">
                <input type="hidden" id="viewVar" name="view" value="bfg" />
                <input type="hidden" id="tooltipVar" name="tooltips" value="yes" />
                <input type="hidden" id="presentationVar" name="presentation" value="no" />
                <select id="example" name="example">
                    <?php
                    foreach (new DirectoryIterator('./bfg_examples/') as $fileInfo) {
                        if (!$fileInfo->isFile())
                            continue;
                        $fullFilename = $fileInfo->getFilename();
                        $extension = pathinfo($fullFilename, PATHINFO_EXTENSION);
                        if ($extension === "svg") {
                            $filename = pathinfo($fullFilename, PATHINFO_FILENAME);
                            $exampleName = str_replace("_", " ", $filename);
                            if ($bfgExample == ("./bfg_examples/" . $fullFilename)) {
                                echo "<option value='" . $filename . "' selected='selected'>" . $exampleName . "</option>";
                            } else {
                                echo "<option value='" . $filename . "'>" . $exampleName . "</option>";
                            }
                        }
                    }
                    ?>
                </select>
            </form></div>
        </div>
        
        <div id="bfg" class="viewBfg">
            <embed src="<?= $bfgExample ?>" type="image/svg+xml" style="height: 100%; width: 100%; cursor: pointer;" />
            <div id="note" class="tooltip">
                Click and drag the graph to move, use scroll wheel to zoom. <span id="closeNote" style="cursor: pointer; color: black">Close!</span>
            </div>
        </div>
        
        <p>The image <a href="#bfg">above</a> is an automatically generated Boolean flow graph extracted from the Boolean program <a href="#bp">below</a>:</p>
        
        <div id="bp" class="viewBp">
            <pre><?php
                if (file_exists($bpExample)) {
                    require_once($bpExample);
                } else {
                    echo "The code-file for this example does not exist!";
                }
                ?></pre>
        </div>
        
        <p>Thanks to <a href="http://code.google.com/p/svgpan/">svgPan</a> and <a href="http://www.graphviz.org/">Graphviz</a>!</p>
        <p>/<a href="http://www.wikander.net">Simon Wikander</a></p>
        
    </body>
</html>